# -*- coding: utf-8 -*-

from scrapy.contrib.pipeline.images import ImagesPipeline
from scrapy.http import Request


class AitaotuPipeline(object):
    def process_item(self, item, spider):
        # 保存数据库
        return item

class AitaotuPipeline(ImagesPipeline):

    def get_media_requests(self, item, info):
        image_url = item['image_url']
        yield Request(image_url, meta={'item': item})

    def file_path(self, request, response=None, info=None):
        item = request.meta['item']
        # 分类名称
        # 图片名称
        category_name = item['category']
        img_name = item['image_url'].split('/')[-1]
        path = category_name + '/' + img_name
        return path

    def item_completed(self, results, item, info):
        print(results)
        return item
